package controller.admin;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.ejb.EJB;
import model.*;
import java.util.*;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.bean.ManagedProperty;

@Named(value = "a_roleManagedBean")
@SessionScoped
public class RoleManagedBean implements Serializable {

    @EJB
    private RoleFacade roleFacade;
    @EJB
    private RoleRightsFacade roleRightFacade;
    private List<Role> list = new ArrayList<Role>();
    private Role obj = new Role();
    private String Name;

    public List<Role> getList() {
        return list;
    }

    public void setList(List<Role> list) {
        this.list = list;
    }

    public Role getObj() {
        return obj;
    }

    public void setObj(Role obj) {
        this.obj = obj;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    /**
     * Creates a new instance of RoleManagedBean
     */
    public RoleManagedBean() {
    }

    /**
     * ********************************************
     */
    public void create() {
        try {
            obj = new Role();
            obj.setName(Name);

            this.roleFacade.create(obj);
            set_list();
            Name = "";

            set_message(FacesMessage.SEVERITY_INFO, "Create Role", "Create " + obj.getName() + " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Create Role", "Create " + obj.getName() + " error !!!");
        }
    }

    public void edit() {
        try {
            this.roleFacade.edit(obj);
            RightsManagedBean rmb = this.get_RightsManagedBean();
            
            List<Integer> RightIDList = new ArrayList<Integer>();
            for(RoleRights rr : this.roleRightFacade.findByIDRole(obj.getId()))
            {
                RightIDList.add(rr.getRoleRightsPK().getIDRights());
            }
            
            List<Integer> selectListRight = new ArrayList<Integer>();
            next_int:            
            for(String s : rmb.getSelectListRights())
            {
                Integer i = Integer.parseInt(s);
                selectListRight.add(i);
                for(RoleRights rr : this.roleRightFacade.findByIDRole(obj.getId())){
                    if(i==rr.getRoleRightsPK().getIDRights()){
                        rr.setAgree(true);
                        this.roleRightFacade.edit(rr);
                        continue next_int;
                    }
                }
                RoleRights rr2 = new RoleRights(i, obj.getId());
                rr2.setAgree(true);
                this.roleRightFacade.create(rr2);
            }
            RightIDList.removeAll(selectListRight);
            for(Integer i : RightIDList){
                RoleRights rr = new RoleRights(i, obj.getId());
                rr.setAgree(false);
                this.roleRightFacade.edit(rr);
                //this.roleRightFacade.remove(rr);
            }
            set_list();
            //set_message(FacesMessage.SEVERITY_INFO, "Edit Role", "try 14");
            set_message(FacesMessage.SEVERITY_INFO, "Edit Role", "Edit " + obj.getName() + " successfull.");
        } catch (Exception e) {
            //set_message(FacesMessage.SEVERITY_WARN, "Edit Role", "try 14");
            set_message(FacesMessage.SEVERITY_WARN, "Edit Role", "Edit " + obj.getName() + " error !!!");
        }
    }

    public void remove(Role r) {
        try {
            this.roleFacade.remove(r);
            set_list();

            set_message(FacesMessage.SEVERITY_INFO, "Remove Role", "Remove " + obj.getName() + " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Remove Role", "Remove " + obj.getName() + " error !!!");
        }
    }

    /**
     * ********************************************
     */
    public void set_list() {
        try {
            list = this.roleFacade.findAll();
        } catch (Exception e) {
            list = null;
        }
    }

    private void set_message(FacesMessage.Severity severity, String title, String message) {
        try {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, title, message));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error message !!!"));
        }
    }

    private RightsManagedBean get_RightsManagedBean() {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        RightsManagedBean rmb = (RightsManagedBean) FacesContext.getCurrentInstance().getApplication()
                .getELResolver().getValue(elContext, null, "a_rightsManagedBean");
        return rmb;
    }
}
